'''
Created on Dec 4, 2009

@author: tliu
'''
from google.appengine.ext import db
from Subscription import Subscription 

def createAuction(a_description, a_photo_link, a_minimum_price, a_direct_URL, project):
    auction = Auction(description = a_description,
                  photo = a_photo_link,
                  minimum_price = a_minimum_price,
                  url = a_direct_URL,
                  project_ref = project
                  )
    auction.put()
    return auction
    
class Auction(Subscription):
    from Project import Project
    description = db.StringProperty(required=True)
    photo = db.BlobProperty(required = True)
    minimum_price = db.FloatProperty(required=True)
    url =db.LinkProperty(required = True)
    permission_tag = db.StringListProperty()
    bids = db.ListProperty(db.Key)
    project_ref = db.ReferenceProperty(Project, required=True)
    
    def updateDescription(self, description):
        self.description = description
        self.put()
    
    def updatePhotoLink(self, photo_link):
        self.photo_link = photo_link
        self.put()
    
    def updateMinimumPrice(self, minimum_price):
        self.minimum_price = minimum_price
        self.put()
        
    def updateDirectLink(self, direct_link):
        self.direct_URL = direct_link
        self.put()
        
    def updatePermissionTag(self, list_of_tags):    
        self.permission_tag = list_of_tags
        self.put()
        
    def addBid(self, bid):
        self.bids.append(bid.key())
        self.put()
    
    def getBids(self):
        from Bid import Bid
        return map(lambda x:Bid.get(x), self.bids)
    
    def getItems(self):
        item_list = []
        for bid in self.getBids():
            item_list.append({"title" : "Bid on %s Auction" % self.project_ref.name,
                              "date" : bid.date,
                              "content" : "New Price: %d" % bid.amount,
                              "author" : bid.bidding_user.name,
                              "url" : self.url,
                              "footer" : ""})  
        return item_list